package com.hanlink.dsp.model;

import android.content.Context;

import com.edge.pbxdsp.R;
import com.hanlink.dsp.ca.model.channel.SummingChannel;
import com.hanlink.dsp.DspSettings;

import com.hanlink.dsp.ca.model.channel.EqChannel;
import com.hanlink.dsp.ca.model.channel.LevelChannel;
import com.hanlink.dsp.ca.model.channel.XoverChannel;
import com.hanlink.dsp.ca.model.settings.AdvanceSettings;
import com.hanlink.dsp.ca.model.settings.EqSettings;
import com.hanlink.dsp.ca.model.settings.HomeSettings;
import com.hanlink.dsp.ca.model.settings.LedSettings;
import com.hanlink.dsp.ca.model.settings.PlaySettings;
import com.hanlink.dsp.ca.model.settings.XoverSettings;
import com.hanlink.dsp.ui.widget.AdvanceChannel;
import com.hanlink.dsp.util.Exception.DBNameNotUniqueException;
import com.qztech.utils.YzDebug;

import org.xutils.DbManager;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.ex.DbException;

/**
 * Created by zhb on 2017/8/4.
 */

public class LocalStorage {
    private Class<? extends BaseSettings>[] settings = new Class[]{HomeSettings.class,EqSettings.class, XoverSettings.class, AdvanceSettings.class, LedSettings.class, PlaySettings.class};

    public void deleteSettings(Context context, DbManager db, int ref_id) {
        try {
            db.deleteById(UserProfile.class, ref_id);

            deleteChannelBySettingId(db, EqChannel.class, DspSettings.EQ_SETTINGS.getId());
            deleteChannelBySettingId(db, SummingChannel.class, DspSettings.SUMMING_SETTINGS.getId());
            deleteChannelBySettingId(db, XoverChannel.class, DspSettings.XOVER_SETTINGS.getId());
            deleteChannelBySettingId(db, AdvanceChannel.class, DspSettings.ADVANCE_SETTINGS.getId());

            for (int i = 0; i < settings.length; i++) {
                db.delete(settings[i], WhereBuilder.b("_ref_id", "=", ref_id));
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    private void deleteChannelBySettingId(DbManager db, Class clazz, int settingId) throws DbException {
        if (settingId > 0){
            int ret = db.delete(clazz, WhereBuilder.b("settings_id", "=", settingId));
            YzDebug.d("deleteChannelBySettingId:" + clazz.getSimpleName() + " id:" + settingId);

        }

    }

    public boolean saveSettings(Context context, DbManager db) throws DBNameNotUniqueException {
        try {

//            db.saveOrUpdate(DspSettings.getUserProfile());
            db.saveOrUpdate(DspSettings.HOME_SETTINGS);
            db.saveOrUpdate(DspSettings.EQ_SETTINGS);
            db.saveOrUpdate(DspSettings.XOVER_SETTINGS);
//            db.saveOrUpdate(DspSettings.SUMMING_SETTINGS);

            for (EqChannel eqChannel : DspSettings.EQ_SETTINGS.getEqChannels()) {
                db.saveOrUpdate(eqChannel);
            }
            for (XoverChannel channel : DspSettings.XOVER_SETTINGS.getXoverChannels()) {
                db.saveOrUpdate(channel);
            }

            for (LevelChannel channel : DspSettings.ADVANCE_SETTINGS.getChannel()) {
                db.saveOrUpdate(channel);
            }

//            for (SummingChannel channel : DspSettings.SUMMING_SETTINGS.getSummingChannels()) {
//                db.saveOrUpdate(channel);
//            }
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            throw new DBNameNotUniqueException(context.getResources().getString(com.hanlink.res.R.string.db_name_not_unique));
        }
    }
}
